package com.youku.component;

import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.youku.expression.Expression;
import com.youku.expression.Position;
import com.youku.util.Consts;

public class Validation implements CaseStep {

	private List<Expression> expressions = new LinkedList<>();

	public Validation(Position position, String expressionString) {
		String[] expArr = StringUtils.split(expressionString, ';');
		for (String singleExp : expArr) {
			// 给表达式添加openToken和closeToken
			expressions.add(new Expression(position, new StringBuilder().append(Consts.NORMAL_OPEN_TOKEN)
					.append(singleExp).append(Consts.NORMAL_CLOSE_TOKEN).toString()));
		}
	}

	public Validation() {
	}

	@Override
	public void run() {
		// 验证
		for (Expression expression : expressions) {
			expression.get();
		}
	}

	@Override
	public String getKey() {
		return null;
	}

	@Override
	public void updatePosition(Position position) {
		for (Expression expression : expressions) {
			expression.setPosition(position);
		}
	}

	@Override
	public void setScope(BaseAttribute attribute) {
		// TODO validation的作用域，无需填写，无意义
	}

	@Override
	public CaseStep cloneStep() {
		Validation validation = new Validation();
		validation.expressions = new LinkedList<>();
		for (Expression expression : expressions) {
			validation.expressions.add(new Expression(expression.getPosition(), expression.getExpressionString()));
		}
		return validation;
	}

}
